$(function () {
    //领料公司加载
    $("[name='lld.llgs.id']").jgbmtreewithnotqx($("[name='lld.llbm.id']"), $("[name='lld.llr.id']"),
        $("#lld_llbm").val(), $("#lld_llr").val())
    //发料公司加载
    $("[name='lld.flgs.id']").jgbmtreewithnotqx($("[name='lld.flbm.id']"), undefined,
        $("#lld_flbm").val(), undefined)
    //加载到货仓库
    $("[name='lld.thck.id']").ckcombobox();

    $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm':'cd'}, function (cddata) {
        $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm':'jzfs'}, function (data) {
            lldmxGrid($.webPath() + '/kcgl/nbll/nbll_lldmx?lldId=' + $("#lld_blcontent_id").val() || '', cddata, data);
        })
    });

})
function lldmxGrid(href, cdOptions, fsOptions) {
    $("#lld_mx").edatagrid({
        url:href,
        fitColumns:false,
        singleSelect:true,
        fit:true,
        checkOnSelect:false,
        selectOnCheck:false,
        showFooter:true,
        rownumbers:true,
        toolbar:"#lld_lldmx_toolbar",
        columns:[
            [
                {field:'id', title:'ID', hidden:true, rowspan:2},
                {field:'wzgg.code', title:'物资编码', width:100, rowspan:2},
                {field:'wzgg.name', title:'物资名称', width:100, rowspan:2},
                {field:'wzgg.ggxh', title:'规格型号', width:100, rowspan:2},
                {field:'wzgg.dw', title:'单位', width:70, rowspan:2},
                {field:'cd.z', title:'产地', width:70, rowspan:2},
                {field:'chd', title:'长度', width:70, rowspan:2},
                {field:'jz', title:'件重', width:60, rowspan:2},
                {field:'jzfs.z', title:'计重方式', width:50, rowspan:2},
                {field:'kddj', title:'成本价(不含税)', width:100, formatter:function (value) {
                    var sl = formatJe(value, ",");
                    return sl;
                }, rowspan:2},
                {title:'开单', colspan:2},
                {title:'发料', colspan:2},
                {field:'llbz', editor:{type:'text'}, title:'备注', width:100, rowspan:2}
            ],
            [
                {field:'kdjs', title:'件数', formatter:function (value) {
                    var sl = formatSl(value, ",");
                    return sl;
                }, width:70},
                {field:'kdsl', title:'数量', formatter:function (value) {
                    var sl = formatSl(value, ",");
                    return sl;
                }, width:100},
                {field:'fljs', editor:{type:'numberbox', formatter:function (value) {
                    var sl = formatSl(value, ",");
                    return sl;
                }, options:{min:0, precision:0}}, title:'件数', width:70},
                {field:'flsl', editor:{type:'numberbox', formatter:function (value) {
                    var sl = formatSl(value, ",");
                    return sl;
                }, options:{min:0, precision:4}}, title:'数量', width:100}
            ]
        ],
        onSelect:function (rowIndex, rowData) {
            //获取每个单元格
            $("#lld_mx").edatagrid('editRow', rowIndex);
            var editors = $("#lld_mx").datagrid('getEditors', rowIndex);
            if (editors.length == 0)
                return;
            var flslEditor = editors[0];
            flslEditor.target.unbind('blur').bind('blur', function () {
                $(flslEditor.target).numberbox('setValue', $(flslEditor.target).val());
                countlldSum(rowIndex, $(flslEditor.target).numberbox('getValue'), 'flsl');
            });
        }
    })
}

//计算开单数量和开单金额
function countlldSum(index, count, type) {
    var sumkdsl = 0;
    var sumflsl = 0;
    var data = $("#lld_mx").edatagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var kdsl = parseFloat(data.rows[i]['kdsl']);
        var flsl = parseFloat(data.rows[i]['flsl']);
        kdsl = isNaN(kdsl) ? 0 : kdsl;
        flsl = isNaN(flsl) ? 0 : flsl;
        if (type == 'flsl') {
            if (i == index)
                flsl = parseFloat(count);
        }
        sumkdsl += kdsl;
        sumflsl += flsl;
    }
    addFooter(sumkdsl, sumflsl);

}

//在表底加载开单数量和开单金额的合计
function addFooter(sumkdsl, sumflsl) {
    var footer = [
        {"wzgg.code":"合计", "kdsl":formatMoney(sumkdsl), "flsl":formatMoney(sumflsl)}
    ];
    if (footer != null)
        $("#lld_mx").edatagrid('reloadFooter', footer);
}

